﻿using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

using ScreenMonkey.Net;

namespace ScreenMonkey.Remote
{
    /// <summary>
    /// SMPageViewModel code taken from the Screen Monkey remote
    /// sample with only minor changes.
    /// </summary>
    public class SMPageViewModel : DependencyObject
    {
        public SMPageViewModel()
        {
        }

        public SMPageViewModel(PageInformation page)
        {
            Page = page;
            LoadPage();
        }

        private void LoadPage()
        {
            if (Page != null)
            {
                Legend = Page.Name;
            }
            else
            {
                Legend = string.Empty;
            }
        }

        public void LoadThumbnail(byte[] image)
        {
            if (image == null)
            {
                Thumbnail = null;
            }
            else
            {
                PngBitmapDecoder decoder = new PngBitmapDecoder(new MemoryStream(image), BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
                ImageSource source = decoder.Frames[0];
                source.Freeze();
                Thumbnail = source;
            }
        }

        private PageInformation pageInfo = null;

        public PageInformation Page
        {
            get { return pageInfo; }
            set
            {
                if (pageInfo != value)
                {
                    pageInfo = value;
                    LoadPage();
                }
            }
        }

        #region View Model

        public string Legend
        {
            get { return (string)GetValue(LegendProperty); }
            set { SetValue(LegendProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Legend.
        // This enables animation, styling, binding, etc.
        public static readonly DependencyProperty LegendProperty =
            DependencyProperty.Register("Legend", 
                                        typeof(string), 
                                        typeof(SMPageViewModel),
                                        new UIPropertyMetadata(string.Empty));

        public ImageSource Thumbnail
        {
            get { return (ImageSource)GetValue(ThumbnailProperty); }
            set { SetValue(ThumbnailProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Thumbnail.
        // This enables animation, styling, binding, etc.
        public static readonly DependencyProperty ThumbnailProperty =
            DependencyProperty.Register("Thumbnail",
                                        typeof(ImageSource), 
                                        typeof(SMPageViewModel), 
                                        new UIPropertyMetadata(null));


        #endregion
    }
}
